package 课程设计报告_hb_7_7;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.Test;

/**
 * @author Jiang Zhiyan
 *
 * 2021年7月7日 上午9:08:28
 *
 */
public class ObjectStreamTest {
     
	@Test
	public void createObjectStreamTest() throws Exception{
		try(FileOutputStream fos = new FileOutputStream("./demo/stu.obj");
				ObjectOutputStream oos = new ObjectOutputStream(fos);
				){
			Student s1 = new Student("jzy","18","互联网");
			oos.writeObject(s1);
		}
	}
	
	@Test
	public void readObjectStreamTest() throws Exception{
		try(FileInputStream fis = new FileInputStream("./demo/stu.obj");
				ObjectInputStream ois = new ObjectInputStream (fis);){
			Object obj = ois.readObject();
			if(obj instanceof Student) {
				Student stu = (Student) obj;
				System.out.println(stu.getName());
			}
		}
	}
}
